package com.runoob.thread_exec;

public class ThreadDemo extends Thread {
    private Thread t;
    private final String threadName;

    public ThreadDemo(String threadName) {
        this.threadName = threadName;
        System.out.println("Create Thread: " + threadName);
    }

    public void run() {
        System.out.println("Running " + threadName);
        try {
            // 创建多个线程
            for (int i = 4; i > 0; i--) {
                System.out.println("Thread: " + threadName + " , " + i);
                // 让线程睡眠一会
                Thread.sleep(50);
            }
        } catch (InterruptedException e) {
            System.out.println("Thread " + threadName + " interrupted!");
        }

        System.out.println("Thread" + threadName + " execting!");
    }

    public void start() {
        System.out.println("Starting " + threadName);
        if (t == null) {
            t = new Thread(this, threadName);
            t.start();
        }
    }
}
